import 'package:http/http.dart' as http;
import 'dart:convert';

class DataLoader {
  bool _isLoading = false;
  final bool _hasMoreData = true;

  static const String baseUrl = 'https://hht.gzsjcgm.com';

  Future<List<Map<String, dynamic>>?> _loadData(String path) async {
    if (!_isLoading && _hasMoreData) {
      _isLoading = true;
      final response = await http.get(Uri.parse('$baseUrl$path'));
      if (response.statusCode == 200) {
        // if (path.startsWith("/search?val=")) {
        //   print('url $baseUrl$path ,dataList :${response.body}');
        // }
        List<dynamic> data = json.decode(response.body);
        List<Map<String, dynamic>> dataList =
            List<Map<String, dynamic>>.from(data);
        _isLoading = false;
        return dataList;
      } else {
        _isLoading = false;
        throw Exception('Failed to load data');
      }
    }
    return null;
  }

  Future<Map<String, dynamic>?> _loadDataMap(String path) async {
    if (!_isLoading && _hasMoreData) {
      _isLoading = true;
      String url = '$baseUrl$path';
      var response = (await http.get(Uri.parse(url)));
      if (response.statusCode == 200) {
        var data = json.decode(response.body);
        Map<String, dynamic> dataMap = Map<String, dynamic>.from(data);
        _isLoading = false;
        return dataMap;
      } else {
        _isLoading = false;
        throw Exception('Failed to load data');
      }
    }
    return null;
  }

  Future<List<Map<String, dynamic>>?> loadTabData(
      {required String kind, required int tid}) async {
    String url = '/hot/hand?tid=$tid&index=1';
    if (tid > 4) {
      kind = kind.substring(0, 2);
      url = '/class/list?class=$kind';
    } else if (tid == -1) {
      kind = '热门';
      url = '/class/list?class=$kind&page=100';
    }
    return _loadData(url);
  }

  Future<Map<String, dynamic>> loadVideoInfo({required int vid}) async {
    return (await await _loadDataMap('/getplay?id=$vid'))
        as Map<String, dynamic>;
  }

  Future<List<Map<String, dynamic>>> loadRecommendation(
      {required int tid}) async {
    String url = '/getlist?typeid=$tid&page=1';
    return (await await _loadData(url)) as List<Map<String, dynamic>>;
  }

  Future<List<Map<String, dynamic>>> loadPageData(
      {required int tid, required int page}) async {
    String url = '/getlist?typeid=$tid&page=$page';
    return (await _loadData(url)) as List<Map<String, dynamic>>;
  }

  Future<List<Map<String, dynamic>>?> loadHotList({required int tid}) async {
    String url = tid == 0 ? '/hot/hits' : '/hot/hits?tid=$tid';
    return _loadData(url);
  }

  Future<List<Map<String, dynamic>>?> loadSearchList(
      {required String search}) async {
    String url = '/search?val=$search';
    return _loadData(url);
  }

  Future<Map<String, dynamic>?> loadVideoLocalAddr(
      {required String addr}) async {
    if (!_isLoading && _hasMoreData) {
      _isLoading = true;
      String url = 'https://gd1.2000000000line.com/main/jiexi?m3u8=$addr';
      var response = (await http.get(Uri.parse(url)));
      if (response.statusCode == 200) {
        var data = json.decode(response.body);
        Map<String, dynamic> dataMap = Map<String, dynamic>.from(data);
        _isLoading = false;
        return dataMap;
      } else {
        _isLoading = false;
        throw Exception('Failed to load data');
      }
    }
    return null;
  }

  Future<Map<String, dynamic>?> loadVideoAddr({required String addr}) async {
    if (!_isLoading && _hasMoreData) {
      _isLoading = true;
      String url = 'https://api.blmcp.com/m3u8.php?url=$addr';
      var response = (await http.get(Uri.parse(url)));
      if (response.statusCode == 200) {
        var data = json.decode(response.body);
        Map<String, dynamic> dataMap = Map<String, dynamic>.from(data);
        _isLoading = false;
        return dataMap;
      } else {
        _isLoading = false;
        throw Exception('Failed to load data');
      }
    }
    return null;
  }

// https://api.blmcp.com/m3u8.php?url=znjson-qq-2148780169-1_0bc3gqacoaaa5uankamlabsvcngde42aaj2a
//https://gd1.2000000000line.com/main/jiexi?m3u8=http://127.0.0.1:10011/m3u8/aec3e445-66f3-47fa-ab9e-08f1aec5e77c/1.m3u8
}
